import sys
import os
import time
import subprocess
from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QSplitter, QTreeView, QTabWidget, QStatusBar, 
    QVBoxLayout, QWidget, QTextEdit, QGraphicsView, QGraphicsScene, QGraphicsRectItem,
    QToolBar, QMessageBox, QPushButton, QLabel, QMenu, 
    QInputDialog, QFileDialog, QDialog, QDialogButtonBox, QSizePolicy
)
from PyQt6.QtGui import (
    QIcon, QTextCharFormat, QColor, QFont, QPen, QBrush, QPainterPath, QFileSystemModel,
    QTextCursor, QPalette, QStandardItemModel, QStandardItem, QSyntaxHighlighter, 
    QRegularExpressionValidator, QAction, QPainter
)
from PyQt6.QtCore import (
    Qt, QRectF, QPointF, QModelIndex, QFileInfo, QSortFilterProxyModel,
    QRegularExpression, QTimer, QSize
)
import git
from git import Repo, GitCommandError, InvalidGitRepositoryError




# ===================== 状态仪表盘 =====================
class StatusDashboard(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        layout = QVBoxLayout()
        
        # 分支信息
        self.branch_label = QLabel("分支: 无")
        self.branch_label.setStyleSheet("""
            QLabel {
                font-size: 16px;
                font-weight: bold;
                color: #569cd6;
            }
        """)
        layout.addWidget(self.branch_label)
        
        # 更改状态
        self.status_label = QLabel("状态: 无更改")
        self.status_label.setStyleSheet("""
            QLabel {
                font-size: 14px;
                color: #d4d4d4;
                margin-top: 5px;
            }
        """)
        layout.addWidget(self.status_label)
        
        # 文件状态
        self.files_layout = QVBoxLayout()
        files_group = QWidget()
        files_group.setLayout(self.files_layout)
        layout.addWidget(files_group)
        
        layout.addStretch()
        self.setLayout(layout)
    
    def update_branch(self, branch_name):
        self.branch_label.setText(f"分支: {branch_name}")
    
    def update_status(self, status):
        # 清除之前的文件状态
        for i in reversed(range(self.files_layout.count())):
            self.files_layout.itemAt(i).widget().deleteLater()
        
        # 显示更改状态
        changed_count = len(status['unstaged']) + len(status['untracked'])
        staged_count = len(status['staged'])
        
        if changed_count == 0 and staged_count == 0:
            self.status_label.setText("状态: 无更改")
            return
        
        self.status_label.setText(f"状态: {staged_count}已暂存 | {changed_count}未提交")
        
        # 添加已暂存文件
        if staged_count > 0:
            staged_label = QLabel("已暂存文件:")
            staged_label.setStyleSheet("color: #4ec9b0; font-weight: bold; margin-top: 10px;")
            self.files_layout.addWidget(staged_label)
            
            for file in status['staged']:
                file_label = QLabel(f"  ✓ {file}")
                file_label.setStyleSheet("color: #4ec9b0;")
                self.files_layout.addWidget(file_label)
        
        # 添加未提交文件
        if status['unstaged'] or status['untracked']:
            unstaged_label = QLabel("未提交文件:")
            unstaged_label.setStyleSheet("color: #d7ba7d; font-weight: bold; margin-top: 10px;")
            self.files_layout.addWidget(unstaged_label)
            
            for file in status['unstaged']:
                file_label = QLabel(f"  ● {file}")
                file_label.setStyleSheet("color: #d7ba7d;")
                self.files_layout.addWidget(file_label)
            
            for file in status['untracked']:
                file_label = QLabel(f"  ? {file}")
                file_label.setStyleSheet("color: #808080;")
                self.files_layout.addWidget(file_label)